-- UIBeelineTrap
-- Created by chengb Mar/21/2016
-- 直线型陷阱界面

UIBeelineTrap = class("UIBeelineTrap", function()
    return cc.Layer:create();
end);

function UIBeelineTrap.create(gridData, okFunc)
    return UIBeelineTrap.new(gridData, okFunc);
end

function UIBeelineTrap:ctor(gridData, okFunc)
    -- 初始化
    self:setName("UIBeelineTrap");
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData = gridData;
    self.okFunc = okFunc;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBeelineTrap:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBeelineTrap:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBeelineTrap 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBeelineTrap:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBeelineTrap");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            if type(self.okFunc) == 'function' then
                self.okFunc();
            end

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBeelineTrap");
        end
    end
    AddTouchEventListener(btnOk, onOkClick);
end

-- 重绘
function UIBeelineTrap:redraw()
    local trapId = self.gridData.id;
    local trapInfo = CommonTrapM.query(trapId);

    -- 设置标题
    local title = trapInfo.name;
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end
    titleLabel:setString(title);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 设置图片
    local iconImg = findChildByName(self.node, "CT/icon");
    local iconPath = getLevelItemIconPath("trap");
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    local trapId = self.gridData.id;
    local trapAlias = CommonTrapM.query(trapId, "alias");

    -- 如果是猎人陷阱，还要绘制弓箭
    if trapAlias == "hunter_trap" then
        local arowImg = ccui.ImageView:create("images/ui/level/item/arow.png");
        local bowImg  = ccui.ImageView:create("images/ui/level/item/bow.png");
        iconImg:addChild(bowImg);
        iconImg:addChild(arowImg);
        arowImg:setPosition(iconSize.width/2, iconSize.height/2);
        bowImg:setPosition(iconSize.width/2, iconSize.height/2);

        -- 旋转箭头方向
        local direction =  self.gridData.direction;

        if direction == "up" then
            -- 上，不需要旋转
        elseif direction == "right" then
            -- 右
            arowImg:setRotation(90);
            bowImg:setRotation(90);
        elseif direction == "down" then
            -- 下
            arowImg:setRotation(180);
            bowImg:setRotation(180);
        elseif direction == "left" then
            -- 左
            arowImg:setRotation(270);
            bowImg:setRotation(270);
        end
    -- 弩矢陷阱
    elseif trapAlias == "arrow_trap" then
        -- 绘制弓
        local bowImg  = ccui.ImageView:create("images/ui/level/item/bow.png");
        iconImg:addChild(bowImg);
        bowImg:setPosition(iconSize.width/2, iconSize.height/2);
         -- 旋转箭头方向
        local direction =  self.gridData.direction;

        if direction == "up" then
            -- 上，不需要旋转
        elseif direction == "right" then
            -- 右
            bowImg:setRotation(90);
        elseif direction == "down" then
            -- 下
            bowImg:setRotation(180);
        elseif direction == "left" then
            -- 左
            bowImg:setRotation(270);
        end
    end

    iconImg:setScale(1.3);

    -- 设置描述
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    applyString(descLabel, trapInfo.dialog);

    -- 绘制确定按钮
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    btnOk:setTitleText(trapInfo.button_text);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 绘制返回按钮
    local btnBack = findChildByName(self.node, "BT/btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end










